<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Comments/classes/PhpFox_ComponentMyComments.class.php, $Author:  $ */



/** Component for viewing user comments
 *
 * @version $Id: PhpFox_ComponentMyComments.class.php 447 2008-05-12 22:59:28Z phpfoxsv $
 * @package module.comments
 */
class PhpFox_Mod_Comments_ComponentMyComments extends BaseComponent
{
    function process()
    {
        $oCommentItem = &$this->_oModule->getItem('Comment');
        /* @var $oCommentItem PhpFox_Mod_Comments_ItemComment */
        $oSecurityService = App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        $oTpl = &$this->_oModule->getTemplate();
        App::loadClass('Pager');
        $oReq = &App::getRequest();
        $oUrl = &App::getUrl();
        
        if ($iCommentId = $oReq->getInt('cdelete'))
        {
        	$iType = $oReq->get('type');
        	$sType = '';
        	foreach(App::getParam('aCommentId') as $sTypes => $iTypes)
        	{
        		if ($iType == $iTypes)
				{
					$sType = $sTypes;
					break;
				}
        	}
        	
        	if (!empty($sType))
        	{        	
	        	if ( $sType == 'video' )
				{
		  			$aItem = $this->oDatabase->getRow("SELECT cm_id, cm_userid, cm_mainid FROM ". App::getT('videos_comments') ." WHERE cm_id = '". $iCommentId ."'");
		  			if (isset($aItem['cm_userid']) && (($aItem['cm_userid'] == App::getUser('id')) || App::isStaff()))
	            	{           		
	            		$aVideoItem = $this->oDatabase->getRow("SELECT comments FROM ". App::getT('videos') ." WHERE vid_id = '". $aItem['cm_mainid'] ."'");
	
	            		$iNewCount = ($aVideoItem['comments']-1);
	
	            		$this->oDatabase->_delete(App::getT('videos_comments'), "cm_id = '". $iCommentId ."'");
	            		$this->oDatabase->update(App::getT('videos'), array('comments' => $iNewCount), "vid_id = '". $aItem['cm_mainid'] ."'");
	            		$oAccountService->changeUserInfo($oAccountService->getById($aItem['cm_userid']), 'comment', -1, ($aItem['cm_userid'] == App::getUser('id') ? true : false));
	            		
	            		$this->oDatabase->_delete(App::getT('mail_alert'), "type = '6' AND item = '" . $mId . "' AND c_id = '". $iCommentId ."' AND user_id = '" . $aItem['cm_userid'] . "'");
					}
				}
				else
				{
	        		// Get the comment details
	        		$aItem = $this->oDatabase->getRow("SELECT userid, itemid FROM ". App::getT('comments') ." WHERE cid = '". $iCommentId ."'");
					if (isset($aItem['userid']))
					{
		        	    if (($aItem['userid'] == App::getUser('id')) || App::isStaff() || ($sType == 'account' && $aItem['itemid'] == App::getUser('id')))
		        	    {
		        	    	// Load comment Service
		        	    	$oCommentService = &$this->_oModule->getService('Comment');                 	
		        	    	
							// Lets delete the comment
			                $oCommentService->onDelete($iCommentId);
		
			                if ( $sType == 'account' )
			                {
			                	// Update how many comments this specific user has
								$iNewCount = $oCommentService->updateCounts('user', 'c_comments', "id = '". $aItem['itemid'] ."'", App::getParam('aCommentId',$sType), $aItem['itemid']);
		
			                }
			                elseif ( $sType == 'gallery' )
			                {
			                	$oCommentService->updateCounts('main', 'comment', "id = '". $aItem['itemid'] ."'", App::getParam('aCommentId',$sType), $aItem['itemid']);
			                }
			                elseif ( $sType == 'blog' )
			                {
			                	$oCommentService->updateCounts('journal', 'comment', "id = '". $aItem['itemid'] ."'", App::getParam('aCommentId',$sType), $aItem['itemid']);
			                }
			            }
			            $this->oDatabase->_delete(App::getT('mail_alert'), "type = '" . App::getParam('aCommentId',$sType) . "' AND item = '" . $mId . "' AND c_id = '". $iCommentId ."' AND user_id = '" . $aItem['userid'] . "'");
					}
	            }

				$oUrl->clearParams('cdelete');
				$oUrl->clearParams('type');
				App::forward($oUrl->getUrl(), App::format('Mod_Comments.comment_deleted'), 'message', App::getPageName(), 'comment');
        	}
        }        

        $sUser = $oSecurityService->getCurrentUserLogin();
        $iPage = $oReq->getInt('page', 1);
        $iPageSize = App::getParam('public.pagesize');

		$aItemsTotal = $this->oDatabase->getRow("SELECT COUNT(*) AS count FROM ". App::getT('comments') ."".
			" WHERE userid = '". App::getUser('id') ."' ".
			" ORDER BY time DESC ");
		$iCnt = $aItemsTotal['count'];

		$aItems = $oCommentItem->getListing("SELECT c.cid, c.typeid, c.itemid, c.text, c.time, u.user AS user_name FROM ". App::getT('comments') ." AS c ".
			" LEFT JOIN " . App::getT('user') . " AS u ON(c.itemid = u.id) ".
			" WHERE userid = '". App::getUser('id') ."' ".
			" ORDER BY c.time DESC", $iCnt, $iPage, $iPageSize);

        $oPager = &new Pager($iCnt, $iPage, $iPageSize);
        $iFrom = $oPager->getOffset($iPage, $iPageSize);
        $iTo = (($iFrom + $iPageSize) > $iCnt) ? $iCnt : ($iFrom + $iPageSize);

        $oTpl->assignSrc('aItems', $aItems);
        $oTpl->assign(array(
        	'iFrom'            => ($iCnt) ? ($iFrom + 1) : 0,
        	'iTo'              => $iTo,
        	'iCnt'             => $iCnt,
        	'aPaging'          => ($iCnt) ? $oPager->getInfo($oUrl) : '',
            'sGImageUrl'       => App::getParam('gallery.pic.url'),
            'sUImageUrl'       => App::getParam('user.pic.url'),
            'bShowImages'      => App::getSetting('show_comment_images'),
        ));
        return $oTpl->fetch('MyComments.html');
    }
}
?>